package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductRequirementDAO;
import com.ourteam.product.requirement.dao.ProductRequirement;
import com.ourteam.product.requirement.dao.ProductRequirementDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:39
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductRequirementDAOJdbcImpl extends AbstractSpringDAO
    implements IProductRequirementDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ProductDefineId, ParentId, RequestType, RequestCode, RequestName,
            RequestContent, RequestLevel, RequestRisk, ProposerDate, ProposerId,
            CreatorId, CreateDate, LastEditorId, LastEditDate, Status, Remarks,
            SortIndex, Indent, RequestTrigger, Precondition, RequestKind,
            RequestSource, ProposerName, CreatorName, LastEditorName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ProductDefineId,
            _DataObjectProperty.ParentId, _DataObjectProperty.RequestType,
            _DataObjectProperty.RequestCode, _DataObjectProperty.RequestName,
            _DataObjectProperty.RequestContent, _DataObjectProperty.RequestLevel,
            _DataObjectProperty.RequestRisk, _DataObjectProperty.ProposerDate,
            _DataObjectProperty.ProposerId, _DataObjectProperty.CreatorId,
            _DataObjectProperty.CreateDate, _DataObjectProperty.LastEditorId,
            _DataObjectProperty.LastEditDate, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.SortIndex,
            _DataObjectProperty.Indent, _DataObjectProperty.RequestTrigger,
            _DataObjectProperty.Precondition, _DataObjectProperty.RequestKind,
            _DataObjectProperty.RequestSource, _DataObjectProperty.ProposerName,
            _DataObjectProperty.CreatorName, _DataObjectProperty.LastEditorName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductRequirement, ProductRequirementDAOQueryBean> daoHelper;

    public AbstractProductRequirementDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductRequirement, ProductRequirementDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" PROPOSER ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "PROPOSER."));
            relatedJoinSQLBuffer.append(" = ").append(ProposerId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" CREATOR ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "CREATOR."));
            relatedJoinSQLBuffer.append(" = ").append(CreatorId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" LAST_EDITOR ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "LAST_EDITOR."));
            relatedJoinSQLBuffer.append(" = ").append(LastEditorId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductRequirement
         * @throws Exception
         */
    public boolean checkIsUnique(ProductRequirement aProductRequirement)
        throws Exception {
        if (aProductRequirement == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductRequirement.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductRequirement
     * @throws Exception
     */
    public void insert(ProductRequirement aProductRequirement)
        throws Exception {
        super.doInsert(aProductRequirement);
    }

    /**
         * Batch Insert Datas
         * @param aProductRequirement
         * @throws Exception
         */
    public void batchInsert(ProductRequirement[] aProductRequirements)
        throws Exception {
        super.doBatchInsert(aProductRequirements);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductRequirementDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductRequirement aProductRequirement)
        throws Exception {
        delete(aProductRequirement.getId());
    }

    /**
     * Update Data Selective
     * @param aProductRequirement
     * @throws Exception
     */
    public void update(ProductRequirement aProductRequirement)
        throws Exception {
        if (aProductRequirement.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductRequirement);

        ProductRequirement tempObj = findById(aProductRequirement.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductRequirement, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductRequirements[]
     * @throws Exception
     */
    public void batchUpdate(ProductRequirement[] aProductRequirements)
        throws Exception {
        super.doBatchUpdate(aProductRequirements);
    }

    /**
         * Update Data By Condictions
         * @param aProductRequirement
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductRequirement aProductRequirement,
        ProductRequirementDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductRequirement, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductRequirementDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductRequirement aProductRequirement)
        throws Exception {
        if (aProductRequirement.getRequestType() == null) {
            throw new Exception(
                "The ProductRequirement.requestType value is not allow NULL");
        }

        if (aProductRequirement.getRequestCode() == null) {
            throw new Exception(
                "The ProductRequirement.requestCode value is not allow NULL");
        }

        if (aProductRequirement.getRequestName() == null) {
            throw new Exception(
                "The ProductRequirement.requestName value is not allow NULL");
        }

        if (aProductRequirement.getRequestContent() == null) {
            throw new Exception(
                "The ProductRequirement.requestContent value is not allow NULL");
        }

        if (aProductRequirement.getRequestLevel() == null) {
            throw new Exception(
                "The ProductRequirement.requestLevel value is not allow NULL");
        }

        if (aProductRequirement.getRequestRisk() == null) {
            throw new Exception(
                "The ProductRequirement.requestRisk value is not allow NULL");
        }

        if (aProductRequirement.getProposerDate() == null) {
            throw new Exception(
                "The ProductRequirement.proposerDate value is not allow NULL");
        }

        if (aProductRequirement.getCreateDate() == null) {
            throw new Exception(
                "The ProductRequirement.createDate value is not allow NULL");
        }

        if (aProductRequirement.getLastEditDate() == null) {
            throw new Exception(
                "The ProductRequirement.lastEditDate value is not allow NULL");
        }

        if (aProductRequirement.getStatus() == null) {
            throw new Exception(
                "The ProductRequirement.status value is not allow NULL");
        }

        if (aProductRequirement.getRemarks() == null) {
            throw new Exception(
                "The ProductRequirement.remarks value is not allow NULL");
        }

        if (aProductRequirement.getCreatorName() == null) {
            throw new Exception(
                "The ProductRequirement.creatorName value is not allow NULL");
        }

        if (aProductRequirement.getLastEditorName() == null) {
            throw new Exception(
                "The ProductRequirement.lastEditorName value is not allow NULL");
        }

        if (aProductRequirement.getProposerName() == null) {
            throw new Exception(
                "The ProductRequirement.proposerName value is not allow NULL");
        }

        if (aProductRequirement.getRequestTrigger() == null) {
            throw new Exception(
                "The ProductRequirement.requestTrigger value is not allow NULL");
        }

        if (aProductRequirement.getPrecondition() == null) {
            throw new Exception(
                "The ProductRequirement.precondition value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductRequirement findById(final long id) throws Exception {
        ProductRequirementDAOQueryBean queryBean = new ProductRequirementDAOQueryBean();

        ProductRequirementDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductRequirement[] ProductRequirements = query(queryBean);

        if (ProductRequirements.length == 0) {
            return null;
        } else {
            return ProductRequirements[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductRequirement[]
         * @throws Exception
         */
    public ProductRequirement[] query(ProductRequirementDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductRequirementRowMapper());

        return (ProductRequirement[]) result.toArray(new ProductRequirement[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductRequirementDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductRequirementDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductRequirement[] aProductRequirements,
        ProductRequirementDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductRequirements, queryBean);
    }

    public void synchronize(ProductRequirement[] aProductRequirements,
        ProductRequirementDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductRequirement> callBack)
        throws Exception {
        daoHelper.synchronize(aProductRequirements, queryBean, callBack);
    }

    public static class JdbcProductRequirementRowMapper implements RowMapper<ProductRequirement> {
        public ProductRequirement mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductRequirement object = new ProductRequirement();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object productDefineId = null;

                productDefineId = resultSet.getObject(columnIndex++);

                object.setProductDefineIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (productDefineId != null) {
                    setBeanValue(object, _DataObjectProperty.ProductDefineId,
                        productDefineId);
                }

                Object parentId = null;

                parentId = resultSet.getObject(columnIndex++);

                object.setParentIdNull(resultSet.getBoolean(columnIndex++));

                if (parentId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentId, parentId);
                }

                Object requestType = null;

                requestType = resultSet.getObject(columnIndex++);

                object.setRequestTypeNull(resultSet.getBoolean(columnIndex++));

                if (requestType != null) {
                    setBeanValue(object, _DataObjectProperty.RequestType,
                        requestType);
                }

                Object requestCode = null;

                requestCode = resultSet.getObject(columnIndex++);

                object.setRequestCodeNull(resultSet.getBoolean(columnIndex++));

                if (requestCode != null) {
                    setBeanValue(object, _DataObjectProperty.RequestCode,
                        requestCode);
                }

                Object requestName = null;

                requestName = resultSet.getObject(columnIndex++);

                object.setRequestNameNull(resultSet.getBoolean(columnIndex++));

                if (requestName != null) {
                    setBeanValue(object, _DataObjectProperty.RequestName,
                        requestName);
                }

                Object requestContent = null;

                requestContent = resultSet.getObject(columnIndex++);

                object.setRequestContentNull(resultSet.getBoolean(columnIndex++));

                if (requestContent != null) {
                    setBeanValue(object, _DataObjectProperty.RequestContent,
                        requestContent);
                }

                Object requestLevel = null;

                requestLevel = resultSet.getObject(columnIndex++);

                object.setRequestLevelNull(resultSet.getBoolean(columnIndex++));

                if (requestLevel != null) {
                    setBeanValue(object, _DataObjectProperty.RequestLevel,
                        requestLevel);
                }

                Object requestRisk = null;

                requestRisk = resultSet.getObject(columnIndex++);

                object.setRequestRiskNull(resultSet.getBoolean(columnIndex++));

                if (requestRisk != null) {
                    setBeanValue(object, _DataObjectProperty.RequestRisk,
                        requestRisk);
                }

                Object proposerDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    proposerDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    proposerDate = resultSet.getObject(columnIndex++);
                }

                object.setProposerDateNull(resultSet.getBoolean(columnIndex++));

                if (proposerDate != null) {
                    setBeanValue(object, _DataObjectProperty.ProposerDate,
                        proposerDate);
                }

                Object proposerId = null;

                proposerId = resultSet.getObject(columnIndex++);

                object.setProposerIdNull(resultSet.getBoolean(columnIndex++));

                if (proposerId != null) {
                    setBeanValue(object, _DataObjectProperty.ProposerId,
                        proposerId);
                }

                Object creatorId = null;

                creatorId = resultSet.getObject(columnIndex++);

                object.setCreatorIdNull(resultSet.getBoolean(columnIndex++));

                if (creatorId != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorId,
                        creatorId);
                }

                Object createDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    createDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    createDate = resultSet.getObject(columnIndex++);
                }

                object.setCreateDateNull(resultSet.getBoolean(columnIndex++));

                if (createDate != null) {
                    setBeanValue(object, _DataObjectProperty.CreateDate,
                        createDate);
                }

                Object lastEditorId = null;

                lastEditorId = resultSet.getObject(columnIndex++);

                object.setLastEditorIdNull(resultSet.getBoolean(columnIndex++));

                if (lastEditorId != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditorId,
                        lastEditorId);
                }

                Object lastEditDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    lastEditDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    lastEditDate = resultSet.getObject(columnIndex++);
                }

                object.setLastEditDateNull(resultSet.getBoolean(columnIndex++));

                if (lastEditDate != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditDate,
                        lastEditDate);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object indent = null;

                indent = resultSet.getObject(columnIndex++);

                object.setIndentNull(resultSet.getBoolean(columnIndex++));

                if (indent != null) {
                    setBeanValue(object, _DataObjectProperty.Indent, indent);
                }

                Object requestTrigger = null;

                requestTrigger = resultSet.getObject(columnIndex++);

                object.setRequestTriggerNull(resultSet.getBoolean(columnIndex++));

                if (requestTrigger != null) {
                    setBeanValue(object, _DataObjectProperty.RequestTrigger,
                        requestTrigger);
                }

                Object precondition = null;

                precondition = resultSet.getObject(columnIndex++);

                object.setPreconditionNull(resultSet.getBoolean(columnIndex++));

                if (precondition != null) {
                    setBeanValue(object, _DataObjectProperty.Precondition,
                        precondition);
                }

                Object requestKind = null;

                requestKind = resultSet.getObject(columnIndex++);

                object.setRequestKindNull(resultSet.getBoolean(columnIndex++));

                if (requestKind != null) {
                    setBeanValue(object, _DataObjectProperty.RequestKind,
                        requestKind);
                }

                Object requestSource = null;

                requestSource = resultSet.getObject(columnIndex++);

                object.setRequestSourceNull(resultSet.getBoolean(columnIndex++));

                if (requestSource != null) {
                    setBeanValue(object, _DataObjectProperty.RequestSource,
                        requestSource);
                }

                Object proposerName = null;

                proposerName = resultSet.getObject(columnIndex++);

                object.setProposerNameNull(resultSet.getBoolean(columnIndex++));

                if (proposerName != null) {
                    setBeanValue(object, _DataObjectProperty.ProposerName,
                        proposerName);
                }

                Object creatorName = null;

                creatorName = resultSet.getObject(columnIndex++);

                object.setCreatorNameNull(resultSet.getBoolean(columnIndex++));

                if (creatorName != null) {
                    setBeanValue(object, _DataObjectProperty.CreatorName,
                        creatorName);
                }

                Object lastEditorName = null;

                lastEditorName = resultSet.getObject(columnIndex++);

                object.setLastEditorNameNull(resultSet.getBoolean(columnIndex++));

                if (lastEditorName != null) {
                    setBeanValue(object, _DataObjectProperty.LastEditorName,
                        lastEditorName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
